package com.leetcode.merci.lc406;

import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;

/**
 * Created by bowen on 2017/7/27.
 */
public class Solution {
    public int[][] reconstructQueue(int[][] people) {
        if(people.length==0){
            return new int[0][];
        }
        Arrays.sort(people,new Comparator<int[]>(){
            @Override
            public int compare(int[] p1,int[] p2){
                return p1[0]!=p2[0]?p2[0]-p1[0]:p1[1]-p2[1];
            }
        });
        LinkedList<int[]> list=new LinkedList<>();
        for(int i=0;i<people.length;i++){
            list.add(people[i][1],people[i]);
        }
        return list.toArray(new int[people.length][2]);
    }
    public void sort(int[][] source){
        int[] tmp;
        for(int j=0;j<source.length;j++){
            for(int i=0;i<source.length-1;i++){
                if(source[i][0]<source[i+1][0]){
                    tmp=source[i];
                    source[i]=source[i+1];
                    source[i+1]=tmp;
                }
                else if(source[i][0]==source[i+1][0]){
                    if(source[i][1]>source[i+1][1]){
                        tmp=source[i];
                        source[i]=source[i+1];
                        source[i+1]=tmp;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        int [][] people={{0,0},{6,2},{5,5},{4,3},{5,2},{1,1},{6,0},{6,3},{7,0},{5,1}};
        Solution solution=new Solution();
        System.out.println(Arrays.toString(solution.reconstructQueue(people)));
    }
}